package com.client.service.impl;

import com.client.dto.GoodsAndLanguageDto;
import com.client.mapper.FlowerShoppingMapper;
import com.client.pojo.FlowerOrder;
import com.client.pojo.FlowerShopping;
import com.client.service.FlowerGoodsService;
import com.client.service.FlowerOrderService;
import com.client.service.FlowerShoppingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 晚云
 * @since 2023-09-12
 */
@Service
public class FlowerShoppingServiceImpl extends ServiceImpl<FlowerShoppingMapper, FlowerShopping> implements FlowerShoppingService {

    @Autowired
    private FlowerShoppingMapper flowerShoppingMapper;

    @Autowired
    private FlowerOrderService flowerOrderService;

    @Autowired
    private FlowerGoodsService flowerGoodsService;

    /**
     * 购物车查询全部方法
     *
     * @param userId
     * @return
     */
    @Override
    public List<FlowerShopping> selectByUserId(String userId) {
        List<FlowerShopping> list = flowerShoppingMapper.selectByUserId(userId);
        return list;
    }

    /**
     * 删除购物车里面的商品
     * @param goodsId
     * @return
     */
    @Override
    public boolean deleteByGoodsId(String goodsId,String userId) {
        boolean flag = flowerShoppingMapper.deleteByGoodsId(goodsId,userId);
        return flag;
    }

    /**
     * 添加订单和删除购物车数据
     * @param shoppings
     * @return
     */
    @Override
    @Transactional
    public boolean insertListAndDeleteShopping(List<FlowerShopping> shoppings) {
        List<FlowerOrder> flowerOrders = new ArrayList<>();
        for (FlowerShopping shopping : shoppings) {
            FlowerOrder order = new FlowerOrder();
            BeanUtils.copyProperties(shopping,order);
            // 设置属性
            UUID uuid = UUID.randomUUID();
            // 去掉uuid的连字符
            String replace = uuid.toString().replace("-", "");
            order.setOrderId(replace);
            order.setOrderName(shopping.getShoppingName());
            order.setOrderImg(shopping.getShoppingImg());
            order.setOrderPrice(shopping.getShoppingNumprice());
            order.setOrderCount(shopping.getShoppingCount());
            order.setOrderTime(new Date());
            flowerOrders.add(order);
        }
        boolean flag = flowerOrderService.insertOrder(flowerOrders);
        if (!flag){
            return flag;
        }
        // 已经添加到订单表中，可以删除出购物车里面的数据
        boolean bool = flowerShoppingMapper.deleteByUserIdAndGoodsId(shoppings);
        return bool;
    }

    /**
     * 加入购物车
     * @param goodsAndLanguageDto
     * @return
     */
    @Override
    @Transactional
    public boolean joinShopping(GoodsAndLanguageDto goodsAndLanguageDto) {
        if (goodsAndLanguageDto.getGoodsSpec() == null){
            goodsAndLanguageDto.setGoodsSpec("统一");
        }
        goodsAndLanguageDto.setGoodsImg(split_string(goodsAndLanguageDto.getGoodsImg()));
        FlowerShopping flowerShopping = flowerShoppingMapper.selectByGoodsIdAndUserId(goodsAndLanguageDto.getGoodsId(),
                goodsAndLanguageDto.getUserId(),goodsAndLanguageDto.getGoodsSpec());
        boolean flag = false;
        if (flowerShopping == null || !flowerShopping.getShoppingSpec().equals(goodsAndLanguageDto.getGoodsSpec())){
            flag = flowerShoppingMapper.joinShopping(goodsAndLanguageDto);
        }else {
            // 因为是直接加入购物车所以数量只会加一
            flowerShopping.setShoppingCount(flowerShopping.getShoppingCount()+1);
            // 计算总价   总价=单价*数量
            flowerShopping.setShoppingNumprice(BigDecimal.valueOf(flowerShopping.getShoppingCount()).multiply(flowerShopping.getShoppingPrice()));
            flag = flowerShoppingMapper.updateByCountAndPrice(flowerShopping);
        }
        return flag;
    }

    /**
     * 直接购买商品
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public boolean insertOrder(GoodsAndLanguageDto dto) {
        FlowerOrder order = new FlowerOrder();
        // 设置属性
        UUID uuid = UUID.randomUUID();
        // 去掉uuid的连字符
        String replace = uuid.toString().replace("-", "");
        order.setOrderId(replace);
        dto.setGoodsImg(split_string(dto.getGoodsImg()));
        boolean flag = flowerOrderService.saveOrder(dto,order.getOrderId());
        List<FlowerOrder> flowerOrder = flowerOrderService.selectAndCount(dto.getUserId(),dto.getGoodsId());
        System.out.println("订单数据"+flowerOrder);
        flag = flowerGoodsService.updateByCount(dto.getGoodsId(),flowerOrder);
        return flag;
    }

    /**
     * 字符串分割
     * @param goods
     * @return
     */
    public String split_string(String goods){
        String[] split = goods.split(",");
        return split[0];
    }
}
